package com.efast.cafe.portal.controller.doc;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.efast.cafe.framework.bean.EldfTreeBean;
import com.efast.cafe.framework.bean.ResultBean;
import com.efast.cafe.framework.controller.AbstractController;
import com.efast.cafe.framework.exception.ServiceException;
import com.efast.cafe.portal.bean.doc.DocFolderTree;
import com.efast.cafe.portal.entity.doc.DocFolder;
import com.efast.cafe.portal.service.doc.IDocFolderService;
@Controller
@RequestMapping("/doc")
public class DocFolderController extends  AbstractController{

	private IDocFolderService docFolderService;
	@Autowired
	public void setDocFolderService(IDocFolderService docFolderService) {
		this.docFolderService = docFolderService;
	}
	
	/**
	 * 查询企业文件夹树
	 * @param pid
	 * @param type
	 * @param asyn
	 * @return
	 */
	@RequestMapping(value="/companyFolder/list",method=RequestMethod.POST)
	public @ResponseBody ResultBean queryCompanyFolderList(
			@RequestBody Map<String,String> map){
		ResultBean result = new ResultBean(true,null);
		
		String pid=null;
		String type=null;
		if(map.containsKey("id")){
			pid=map.get("id");
		}
		if(map.containsKey("type")){
			type=map.get("type");
		}
		Integer asyn=0;
		Integer parentId = null;
		if(StringUtils.isNotBlank(pid)){
			if(StringUtils.isBlank(type)){
				result.setErrorCode(ResultBean.ERROR_CODE_PARAMETER);
				result.setSuccess(false);
				return result;
			}
			if(!pid.startsWith("company_") && !pid.startsWith("group_") && !pid.startsWith("folder_")){
				result.setErrorCode(ResultBean.ERROR_CODE_PARAMETER);
				result.setSuccess(false);
				return result;
			}
			if(!type.equals("company") && !type.equals("group") && !type.equals("folder")){
				result.setErrorCode(ResultBean.ERROR_CODE_PARAMETER);
				result.setSuccess(false);
				return result;
			}
			parentId = Integer.parseInt(pid.replaceFirst(type+"_", ""));
		}
		try{
			List<EldfTreeBean> list=docFolderService.queryCompanyFolderTree(parentId, asyn, type);
			List<DocFolderTree> listTree=new ArrayList<>();
			for(int i=0;i<list.size();i++){
				DocFolderTree tree=new DocFolderTree();
				tree.setTitle(list.get(i).getTitle());
				tree.setData(list.get(i));
				listTree.add(tree);
			}
			result.setObject(listTree);
			
		}catch(Exception e){
			e.printStackTrace();
			result.setErrorCode(ResultBean.ERROR_CODE_SYSTEM);
			result.setSuccess(false);
		}
		
		return result;
	}
	
	/**
	 * 查看企业文件夹明细
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/companyFolder/detail",method=RequestMethod.POST)
	public @ResponseBody ResultBean queryFolderById(@RequestParam String id){
		ResultBean result = new ResultBean(true,null);
		try{
			if(!id.startsWith("folder")){
				result.setSuccess(false);
				result.setErrorCode(ResultBean.ERROR_CODE_PARAMETER);
				result.setMessage("参数错误");
				return result;
			}
			Integer folderId = Integer.parseInt(id.replaceFirst("folder_", ""));
			result.setObject(docFolderService.getById(folderId));
		}catch(Exception e){
			e.printStackTrace();
			result.setSuccess(false);
			result.setErrorCode(ResultBean.ERROR_CODE_SYSTEM);
		}
		return result;
	}
	 
	/**
	 * 创建企业文件夹
	 * @param pid
	 * @param folderName
	 * @param folderDesc
	 * @return
	 */
	@RequestMapping(value="/companyFolder/create",method=RequestMethod.POST)
	public @ResponseBody ResultBean createCompanyFolder(
			@RequestBody Map<String,String> map){
		ResultBean result = new ResultBean(true,null);
		String pid=null;
		String folderName=null;
		String folderDesc=null;
		if(map.containsKey("pid")){
			pid=map.get("pid");
		}
		if(map.containsKey("folderName")){
			folderName=map.get("folderName");
		}
		if(map.containsKey("folderDesc")){
			folderDesc=map.get("folderDesc");
		}
		try{
			if(StringUtils.isBlank(pid)){
				throw new ServiceException(ResultBean.ERROR_CODE_PARAMETER);
			}
			DocFolder folder = new DocFolder();
			if(!pid.startsWith("group") && !pid.startsWith("folder")){
				//只能在group和folder上创建文件夹
				result.setSuccess(false);
				result.setErrorCode("0001");//
				result.setMessage("只能在工作组或文件夹下创建文件夹");
				return result;
			}
			if(pid.startsWith("group")){
				folder.setGroupId(Integer.parseInt(pid.replaceFirst("group_", "")));
			}else if(pid.startsWith("folder")){
				folder.setParentId(Integer.parseInt(pid.replaceFirst("folder_", "")));
			}
			folder.setFolderName(folderName);
			folder.setFolderDesc(folderDesc);
			docFolderService.addCompanyFolder(folder);
		}catch(Exception e){
			e.printStackTrace();
			result.setSuccess(false);
			if(e instanceof ServiceException){
				result.setErrorCode(e.getMessage());
			}else{
				result.setErrorCode(ResultBean.ERROR_CODE_SYSTEM);
			}
		}
		return result;
	}
	
	/**
	 * 修改企业文件夹
	 * @param id
	 * @param folderName
	 * @param folderDesc
	 * @return
	 */
	@RequestMapping(value="/companyFolder/update",method=RequestMethod.POST)
	public @ResponseBody ResultBean updateCompanyFolder(
			@RequestBody Map<String,String> map){
		ResultBean result = new ResultBean(true,null);
		String id=null;
		String folderName=null;
		String folderDesc=null;
		if(map.containsKey("id")){
			id=map.get("id");
		}
		if(map.containsKey("folderName")){
			folderName=map.get("folderName");
		}
		if(map.containsKey("folderDesc")){
			folderDesc=map.get("folderDesc");
		}
		DocFolder folder = new DocFolder();
		try{
			if(StringUtils.isBlank(id)){
				throw new ServiceException(ResultBean.ERROR_CODE_PARAMETER);
			}
			if(!id.startsWith("folder")){
				result.setSuccess(false);
				result.setErrorCode(ResultBean.ERROR_CODE_PARAMETER);
				result.setMessage("参数错误");
				return result;
			}
			Integer folderId = Integer.parseInt(id.replaceFirst("folder_", ""));
			folder.setId(folderId);
			folder.setFolderName(folderName);
			folder.setFolderDesc(folderDesc);
			docFolderService.updateCompanyFolder(folder);
		}catch(Exception e){
			e.printStackTrace();
			result.setSuccess(false);
			if(e instanceof ServiceException){
				result.setErrorCode(e.getMessage());
			}else{
				result.setErrorCode(ResultBean.ERROR_CODE_SYSTEM);
			}
		}
		return result;
	}
	
	
	/**
	 * 查询我的文件夹树
	 * @param pid
	 * @param asyn
	 * @return
	 */
	@RequestMapping(value="/myFolder/list",method=RequestMethod.POST)
	public @ResponseBody ResultBean queryMyFolderList(
			@RequestBody Map<String,String> map){
		ResultBean result = new ResultBean(true,null);
		String pid=null;
		if(map.containsKey("id")){
			pid=map.get("id");
		}
		Integer asyn=0;
		Integer parentId = null;
		if(StringUtils.isNotBlank(pid)){
			parentId = Integer.parseInt(pid);
		}
		try{
			List<EldfTreeBean> list=docFolderService.queryMyFolderTree(parentId, asyn);
			List<DocFolderTree> listTree=new ArrayList<>();
			for(int i=0;i<list.size();i++){
				DocFolderTree tree=new DocFolderTree();
				tree.setTitle(list.get(i).getTitle());
				tree.setData(list.get(i));
				listTree.add(tree);
			}
			result.setObject(listTree);
		}catch(Exception e){
			e.printStackTrace();
			result.setErrorCode(ResultBean.ERROR_CODE_SYSTEM);
			result.setSuccess(false);
		}
		return result;
	}
	
	
	/**
	 * 创建我的文件夹
	 * @param pid
	 * @param folderName
	 * @param folderDesc
	 * @return
	 */
	@RequestMapping(value="/myFolder/create",method=RequestMethod.POST)
	public @ResponseBody ResultBean createMyFolder(	@RequestBody Map<String,String> map){
		ResultBean result = new ResultBean(true,null);
		String pid=null;
		String folderName=null;
		String folderDesc=null;
		if(map.containsKey("pid")){
			pid=map.get("pid");
		}
		if(map.containsKey("folderName")){
			folderName=map.get("folderName");
		}
		if(map.containsKey("folderDesc")){
			folderDesc=map.get("folderDesc");
		}
		try{
		    if(pid==null){
		    	pid="folder_0";
		    }
			DocFolder folder = new DocFolder();
			if(!pid.startsWith("group") && !pid.startsWith("folder")){
				//只能在group和folder上创建文件夹
				result.setSuccess(false);
				result.setErrorCode("0001");//
				result.setMessage("只能在工作组或文件夹下创建文件夹");
				return result;
			}
			if(pid.startsWith("group")){
				folder.setGroupId(Integer.parseInt(pid.replaceFirst("group_", "")));
			}else if(pid.startsWith("folder")){
				folder.setParentId(Integer.parseInt(pid.replaceFirst("folder_", "")));
			}
			folder.setFolderName(folderName);
			folder.setFolderDesc(folderDesc);
			
			
			docFolderService.addMyFolder(folder);
		}catch(Exception e){
			e.printStackTrace();
			result.setSuccess(false);
			if(e instanceof ServiceException){
				result.setErrorCode(e.getMessage());
			}else{
				result.setErrorCode(ResultBean.ERROR_CODE_SYSTEM);
			}
		}
		return result;
	}
	
	/**
	 * 修改我的文件夹
	 * @param id
	 * @param folderName
	 * @param folderDesc
	 * @return
	 */
	@RequestMapping(value="/myFolder/update",method=RequestMethod.POST)
	public @ResponseBody ResultBean updateMyFolder(	@RequestBody Map<String,String> map){
		ResultBean result = new ResultBean(true,null);
		String id=null;
		String folderName=null;
		String folderDesc=null;
		if(map.containsKey("id")){
			id=map.get("id");
		}
		if(map.containsKey("folderName")){
			folderName=map.get("folderName");
		}
		if(map.containsKey("folderDesc")){
			folderDesc=map.get("folderDesc");
		}
		DocFolder folder = new DocFolder();
		try{
			if(StringUtils.isBlank(id)){
				throw new ServiceException(ResultBean.ERROR_CODE_PARAMETER);
			}
			if(!id.startsWith("folder")){
				result.setSuccess(false);
				result.setErrorCode(ResultBean.ERROR_CODE_PARAMETER);
				result.setMessage("参数错误");
				return result;
			}
			Integer folderId = Integer.parseInt(id.replaceFirst("folder_", ""));
			folder.setId(folderId);
			folder.setFolderName(folderName);
			folder.setFolderDesc(folderDesc);
			docFolderService.updateMyFolder(folder);
		}catch(Exception e){
			e.printStackTrace();
			result.setSuccess(false);
			if(e instanceof ServiceException){
				result.setErrorCode(e.getMessage());
			}else{
				result.setErrorCode(ResultBean.ERROR_CODE_SYSTEM);
			}
		}
		return result;
	}
	
}
